// components/cart/cart.ts
import qs from 'qs'
Component({
  properties: {
    cart: {
      type: Array
    }
  },
  data: {
    price: 0,
    isShowCartList: false
  },
  methods: {
    onClose() {
      this.setData({
        isShowCartList: false
      })
    },
    onShow() {
      this.setData({
        isShowCartList: !this.data.isShowCartList
      })
    },
    onFoodChange(e: any) {
      this.triggerEvent('onCartChange', e.detail)
    },
    onSubmit() {
      if (!this.properties.cart.length) {
        wx.showToast({
          title: '请选择商品',
          icon: 'none'
        })
        return
      }
      wx.navigateTo({
        url: `/pages/submit-order/submit-order?${qs.stringify([this.properties.cart], {
          encode: false
        })}`
      })
    }
  },
  observers: {
    cart: function(cart) {
      const priceArr: number[] = []
      cart.forEach((item: any) => {
        const  price = item.count * item.price * 100
        priceArr.push(price)
      });
      let price = 0
      priceArr.forEach((item) => {
        price += item
      })
      this.setData({
        price
      })
    }
  }
})
